package com.woniu.servicesse.controller;


import com.woniu.servicesse.request.PushRequest;
import com.woniu.servicesse.service.impl.SseServiceImpl;
import com.woniu.servicesse.util.SsePrefixUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@RestController
@Slf4j
@CrossOrigin(origins = "*", allowedHeaders = "*", exposedHeaders = "Content-Disposition")
public class SseController {
    @Autowired
    private SseServiceImpl sseService;

    /**
     * 建立连接
     * @param phone 用户电话号码
     * @param identity 身份类型
     * @return
     */
    @GetMapping("/connect")
    public SseEmitter connect(@RequestParam("phone") String phone, @RequestParam("identity") Integer identity){
        SseEmitter sseEmitter = sseService.connect(phone,identity);
        return sseEmitter;
    }

    /**
     * 发送消息
     * @param pushRequest
     * @return
     */
    @PostMapping("/push")
    public String push(@RequestBody PushRequest pushRequest){
        String pushMsg = sseService.push(pushRequest);
        return pushMsg;
    }

    /**
     * 关闭连接
     * @param userId 用户ID
     * @param identity 身份类型
     * @return
     */
    @GetMapping("/close")
    public String close(@RequestParam("userId") Long userId, @RequestParam("identity") String identity){
        String closeMsg = sseService.close(userId,identity);
        return closeMsg;
    }
}
